<?php
class Application{
	
	private static $started=false;
	private static $comPath=null;
	public static $COMPONENT = "components";
	private static $COMPONENT_OUTPUT=array();
	
	public static function start(){
		if(self::$started) return false;
		self::$started=true;
		self::loadUserSession();
		$com=self::loadComponent();
		if(self::setComponentOutput($com)===false) return;
		$template=self::loadTemplate();
		echo $template->display();
	}

	private static function loadComponent(){
		if(Request::getVar("com")==false) Request::setVar("com","users");
		$router=new Router();
		if(($com=$router->loadComponent())===false) return false;
		return $com;
	}
	
	private static function setComponentOutput($com){
		if($com==false){
			self::$COMPONENT_OUTPUT=array('component'=>null);
		}else{
			$com->accessCheck();
			$task = Request::getVar("task","display");
			if ($task == '') $task = 'display';
			$com->action($com, $task);
			if($com->redirect==true) return false;
			$output=$com->view->get("output");
			self::$COMPONENT_OUTPUT=array('component'=>$output);
		}
		return true;
	}
	
	private static function loadTemplate(){
		$template=new TemplateParser();
		$template->parseTemplate(self::$COMPONENT_OUTPUT);
		return $template;
	}
	
	private static function loadUserSession(){
		session_set_cookie_params(99999999999);
	    session_name('MYSES');
	    session_start();
	
	    // Reset the expiration time upon page load
	    if (isset($_COOKIE['MYSES']))
	    	setcookie($ses, $_COOKIE['MYSES'], time() + $time, "/");
	}
	

	
	
}
?>